package com.example.meterialproject.exts

import androidx.appcompat.app.AppCompatActivity
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.request.ExplainScope
import com.permissionx.guolindev.request.ForwardScope

/**
 *
 * @ClassName: PermissionExt
 * @Author: android 超级兵
 * TODO 权限申请扩展
 */


/**
 * Manifest.permission.CAMERA 相机权限
 * Manifest.permission.READ_EXTERNAL_STORAGE    写
 * Manifest.permission.WRITE_EXTERNAL_STORAGE   读
 * Manifest.permission.READ_CONTACTS   读写联系人
 * Manifest.permission.CALL_PHONE   电话
 */


fun AppCompatActivity.initPermission(
    ps: List<String>,// 需要申请的权限
    grantedList: (List<String>) -> Unit = {}, // 用于记录所有已被授权的权限，
    deniedList: (List<String>) -> Unit = {}, // 用于记录所有被拒绝的权限。
    isSuccess: (Boolean) -> Unit, // 成功返回
) {
    PermissionX.init(this)
        .permissions(ps)
        .onExplainRequestReason { scope: ExplainScope, deniedList: List<String?>? ->
            scope.showRequestReasonDialog(
                deniedList,
                "即将重新申请的权限是程序必须依赖的权限",
                "我已明白",
                "取消"
            )
        }.onForwardToSettings { scope: ForwardScope, deniedList: List<String?>? ->
            scope.showForwardToSettingsDialog(
                deniedList,
                "您需要去应用程序设置当中手动开启权限",
                "我已明白",
                "取消"
            )
        }
        .request { allGranted: Boolean, grantedList: List<String>, deniedList: List<String> ->
            /*
                * 参数一: 所有权限是否通过
                * 参数二: 用于记录所有已被授权的权限，
                * 参数三: 用于记录所有被拒绝的权限。
                */
            isSuccess(allGranted)
            grantedList(grantedList)
            deniedList(deniedList)
        }
}

